package jmu.cyxwcy.librarymanagement.controller;


import jmu.cyxwcy.librarymanagement.pojo.College;
import jmu.cyxwcy.librarymanagement.pojo.Major;
import jmu.cyxwcy.librarymanagement.pojo.Result;
import jmu.cyxwcy.librarymanagement.service.ICollegeService;
import jmu.cyxwcy.librarymanagement.service.IMajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-06-26
 */
@RestController
@RequestMapping("/major")
//专业
public class MajorController {
    @Autowired
    private IMajorService iMajorService;

    @PostMapping("/add")
    public Result add(@RequestBody Major major){
        iMajorService.getBaseMapper().insert(major);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Major major){
        iMajorService.updateById(major);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result delete(Integer majorId){
        iMajorService.removeById(majorId);
        return Result.success();
    }

    @GetMapping("/findbymajornamepartial")
    public Result findMajorByPartialMajorName(@RequestParam String majorNamePartial) {
        List<Major> majors = iMajorService.findByCollegeNamPartial(majorNamePartial);
        return Result.success(majors);
    }

    @GetMapping("/findbycollegename")
    public Result findByCollegeName(@RequestParam String collegeName, @RequestParam String majorNamePartial) {
        List<Major> majors = iMajorService.findByCollegeName(collegeName, majorNamePartial);
        return Result.success(majors);
    }

    @GetMapping("/findbycollegeid")
    public Result<List<Major>> getMajorsByCollegeId(@RequestParam Integer collegeId) {
        List<Major> majors = iMajorService.findByCollegeId(collegeId);
        return Result.success(majors);
    }

    @GetMapping("/findidbyname")
    public Result findIdByMajorName(@RequestParam String majorName) {
        Integer majorId = iMajorService.findIdByMajorName(majorName);
        if (majorId != null) {
            return Result.success(majorId);
        } else {
            return Result.error("未找到对应的专业ID");
        }
    }
}
